/**
 * User admin Store
 * 负责管理user admin相关的状态和数据处理
 */

import { observable, action  } from 'mobx-miniprogram';
import cloudHelper from '../../../helpers/cloudHelper.js';
import pageHelper from '../../../helpers/pageHelper.js';
import fileHelper from '../../../helpers/fileHelper.js';

// 管理员用户管理模块Store
export const userAdminStore = observable({
  // 用户详情数据
  currentUser: null,
  
  // 用户数据导出信息
  exportData: {
    url: '',
    time: '',
    condition: '',
  },
  
  // 加载状态
  loading: {
    detail: false,
    export: false
  },
  
  // Action: 加载用户详情
  loadUserDetail: action(async function(id) {
    if (!id) return;
    
    this.loading.detail = true;
    
    let params = { id };
    let opts = { hint: false };
    
    try {
      const user = await cloudHelper.callCloudData('admin/user_detail', params, opts);
      if (!user) {
        this.currentUser = null;
        return false;
      }
      
      this.currentUser = user;
      return true;
    } catch (err) {
      console.error('获取用户详情失败', err);
      pageHelper.showNoneToast('获取用户详情失败');
      return false;
    } finally {
      this.loading.detail = false;
    }
  }),
  
  // Action: 重置用户详情
  resetUserDetail: action(function() {
    this.currentUser = null;
  }),
  
  // Action: 加载导出数据
  loadExportData: action(async function(isDel = 1) {
    this.loading.export = true;
    
    let params = { isDel };
    let options = { title: 'bar' };
    
    try {
      const data = await cloudHelper.callCloudData('admin/user_data_get', params, options);
      if (!data) return false;
      
      this.exportData = {
        url: data.url,
        time: data.time,
        condition: this.exportData.condition
      };
      
      return true;
    } catch (err) {
      console.error('获取导出数据失败', err);
      return false;
    } finally {
      this.loading.export = false;
    }
  }),
  
  // Action: 设置导出条件
  setExportCondition: action(function(condition) {
    this.exportData.condition = condition;
  }),
  
  // Action: 导出用户数据
  exportUserData: action(async function(fields) {
    this.loading.export = true;
    
    try {
      let options = {
        title: '数据生成中'
      };

      let params = {
        condition: this.exportData.condition,
        fields
      };

      const res = await cloudHelper.callCloudData('admin/user_data_export', params, options);
      await this.loadExportData(0);
      
      return res.total;
    } catch (err) {
      console.error('导出失败', err);
      pageHelper.showNoneToast('导出失败，请重试');
      return 0;
    } finally {
      this.loading.export = false;
    }
  }),
  
  // Action: 删除导出数据
  deleteExportData: action(async function() {
    this.loading.export = true;
    
    try {
      let options = {
        title: '数据删除中'
      };
      
      await cloudHelper.callCloudData('admin/user_data_del', {}, options);
      
      this.exportData.url = '';
      this.exportData.time = '';
      
      return true;
    } catch (err) {
      console.error('删除失败', err);
      pageHelper.showNoneToast('删除失败，请重试');
      return false;
    } finally {
      this.loading.export = false;
    }
  }),
  
  // Action: 打开导出文档
  openExportDoc: action(function() {
    if (this.exportData.url) {
      fileHelper.openDoc('客户数据', this.exportData.url);
      return true;
    }
    return false;
  })
}); 